/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import gestion.DBgestionLibros;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import negocio.Libro;
import negocio.prestamo;

/**
 *
 * @author Luis Felipe Gomez
 */
public class controlLibro {

    private ResultSet rs;
    private DBgestionLibros dbgm;
    Libro l;

    public controlLibro() {
        rs = null;
        dbgm = new DBgestionLibros();
        l = new Libro();

    }

    public int maximoIsbn() {
        int k = dbgm.maxIsbn();
        return k;
    }

    public Libro bucarLibro(int i) {

        rs = dbgm.BuscarIsbn(i);
        try {
            while (rs.next()) {
                l.setIsbn(rs.getInt(1));
                l.setTitulo(rs.getString(2));
                l.setReservado(rs.getInt(3));
                l.setPrestamo(rs.getInt(4));
            }
        } catch (SQLException ex) {
            Logger.getLogger(controlLibro.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (rs == null) {
            System.out.println("no existe el libro ");

        }
        return l;
    }

    public void crearLibro(Libro o) {

        dbgm.libroNuevo(dbgm.maxIsbn() + 1, o.getTitulo(), o.getReservado(), o.getPrestamo());


    }

    public void crearLibroIsbn(Libro o) {
        dbgm.libroNuevo(o.getIsbn(), o.getTitulo(), o.getReservado(), o.getPrestamo());
    }

    public void eliminarLibro(int o) {
        dbgm.eliminarLibros(o);

    }

    public ResultSet todos() {

        rs = dbgm.todosLibros();
        return rs;

    }

    public Object todosLibros() {
        Object[][] o = null;
        try {

            rs = dbgm.todosLibros();
            int b = 0;
            while (rs.next() == true) {
                b = rs.getRow();

            }
            rs = dbgm.todosLibros();
            o = new Object[b][4];
            int temp = 0;
            while (rs.next() == true) {
                o[temp][0] = rs.getInt(1);
                o[temp][1] = rs.getString(2);
                o[temp][2] = rs.getInt(3);
                o[temp][3] = rs.getInt(4);
                temp++;
            }



        } catch (SQLException ex) {
            Logger.getLogger(controlLibro.class.getName()).log(Level.SEVERE, null, ex);
        }
        return o;
    }

    public void modificarTitulo(int i, String r) {

        dbgm.ModificarTitulo(i, r);



    }

    public int countLib() {
        return dbgm.countLib();
    }
    public void editarRes(int i){
        dbgm.editarReserva(i);
    }
}
